<template>
    <div class="login">
        <div class="login-wrapper">
            <div class="login-header">

            </div>
            <div class="login-container">
                <span><svg t="1731571310440" class="icon" viewBox="0 0 1024 1024" version="1.1"
                        xmlns="http://www.w3.org/2000/svg" p-id="3652" width="40" height="40">
                        <path
                            d="M357.353238 279.350496a278.594155 278.594155 0 0 0 93.216025 208.303966 216.636124 216.636124 0 0 0 62.144016 9.063702c119.836775 0 217.33047-97.493695 217.330471-217.330471S632.550054 61.995228 512.713279 61.995228a216.636124 216.636124 0 0 0-62.181213 9.051303 278.594155 278.594155 0 0 0-93.178828 208.303965z"
                            fill="#b4c5ee" p-id="3653"></path>
                        <path
                            d="M511.969336 61.995228c119.836775 0 217.33047 97.493695 217.330471 217.33047s-97.493695 217.318071-217.330471 217.318072-217.33047-97.481296-217.33047-217.293274 97.493695-217.355269 217.33047-217.355268m0-61.995228C357.712811 0.024798 232.668436 125.069173 232.668436 279.350496S357.712811 558.626598 511.969336 558.626598s279.3009-125.044374 279.300901-279.276102S666.275458 0.024798 511.969336 0.024798z"
                            fill="#b4c5ee" p-id="3654"></path>
                        <path
                            d="M918.075275 870.784969a424.208546 424.208546 0 0 0-16.961894-46.397228q-5.405984-12.399046-11.580708-24.512913a426.675956 426.675956 0 0 0-125.453543-148.007407 420.935198 420.935198 0 0 0-252.618154-83.321586 430.246881 430.246881 0 0 0-48.356278 2.715391c-166.14721 67.921972-283.181801 231.167805-283.181802 421.741135h756.007005a425.113676 425.113676 0 0 0-17.854626-122.217392z"
                            fill="#b4c5ee" p-id="3655"></path>
                        <path
                            d="M238.694372 659.641623a30.997614 30.997614 0 0 1-18.598568-55.795705 482.335271 482.335271 0 0 1 35.275285-24.16574 31.001334 31.001334 0 1 1 32.73348 52.658746c-10.588785 6.583893-20.954387 13.63895-30.799229 21.078378a30.836426 30.836426 0 0 1-18.610968 6.224321zM56.527595 1023.999975a30.997614 30.997614 0 0 1-30.997614-30.997614A486.042586 486.042586 0 0 1 146.011507 672.549029a30.997614 30.997614 0 1 1 46.620411 40.854855A424.047358 424.047358 0 0 0 87.537608 993.01476a30.997614 30.997614 0 0 1-31.010013 30.985215zM967.411078 1023.999975a30.997614 30.997614 0 0 1-30.997614-30.997614 422.113107 422.113107 0 0 0-34.804121-168.627019q-5.405984-12.399046-11.580709-24.512913a426.688355 426.688355 0 0 0-125.453543-147.995008 425.287263 425.287263 0 0 0-407.035867-54.357416 30.997614 30.997614 0 1 1-22.553864-57.717557 487.146101 487.146101 0 0 1 466.514089 62.305204 487.877644 487.877644 0 0 1 143.754534 169.494953q7.067456 13.800138 13.279378 28.096237a484.319118 484.319118 0 0 1 39.912528 193.276322 30.997614 30.997614 0 0 1-31.034811 31.034811z"
                            fill="#b4c5ee" p-id="3656"></path>
                    </svg>:<input v-model="username" type="text" class="login-input" placeholder="用户名" /></span>
                <span><svg t="1731571744157" class="icon" viewBox="0 0 1024 1024" version="1.1"
                        xmlns="http://www.w3.org/2000/svg" p-id="6404" width="40" height="40">
                        <path
                            d="M747.776 397.632V203.776A139.84 139.84 0 0 0 607.936 64H418.112a139.776 139.776 0 0 0-139.776 139.776V397.44A127.616 127.616 0 0 0 160 524.48V832a128 128 0 0 0 128 128h448a128 128 0 0 0 128-128V524.48c0-66.752-51.2-120.832-116.224-126.848z m-175.68 321.344v92.48a34.88 34.88 0 0 1-34.88 35.008h-46.4a34.944 34.944 0 0 1-34.944-35.008v-92.48A112.896 112.896 0 0 1 398.72 621.44c0-62.656 51.648-113.536 115.264-113.536s115.264 50.816 115.264 113.536a112.448 112.448 0 0 1-57.152 97.536z m57.216-322.496H398.72V243.328c0-38.592 31.296-69.888 69.888-69.888h90.816c38.592 0 69.888 31.296 69.888 69.888v153.152z"
                            p-id="6405" fill="#b4c5ee"></path>
                    </svg>:<input v-model="password" type="password" class="login-input" placeholder="密码" /></span>

                <button class="login-button" @click="login">登录</button>
            </div>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import JSEncrypt from 'jsencrypt';
import { Message } from 'element-ui';
import Request from "@/utils/request";
export default {
    data() {
        return {
            username: '',
            password: '',
            publicKey: '-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQClgZCalYmfGoODr7KJ6h6P9boC8sOd4ooT8YerHHbajF7m6Eo8tm7uykPdayndU4QXDrLKmpxfVVx2jhz6nvNQ6Hl2eLoaMcEdd/NdInwp0f4xG39J1SL0TFf9SbgmUHnL9ZDVwxhhbR6r0jOxxnPpCZ5Wr4L4vin5DZhFHoW3hQIDAQAB-----END PUBLIC KEY-----'
        };
    },
    methods: {
        async login() {

            if (!this.username) {
                Message.error('用户名不能为空');
                return;
            }

            if (!this.password) {
                Message.error('密码不能为空');
                return;
            }

            try {
                const encrypt = new JSEncrypt();
                encrypt.setPublicKey(this.publicKey); // 设置公钥
                const encryptedPassword = encrypt.encrypt(this.password.trim());

                const response = await Request.get('webUser/login', {
                    params: {
                        username: this.username.trim(),
                        password: encryptedPassword
                    }
                });

                if (response.data.code === 200) {
                    Cookies.set('token', response.data.token);
                    // 用户名
                    Cookies.set('name', response.data.name);
                    // 是否管理员
                    Cookies.set('isAdmin', response.data.isAdmin)
                    // 账号类型
                    Cookies.set('type', response.data.type);
                    // 电站名
                    Cookies.set('station_user', response.data.station_user);
                    // 左上角名称
                    Cookies.set('station_name', response.data.station_name);
                    Cookies.set('county', response.data.county);
                    Cookies.set('routes', JSON.stringify(response.data.routes));
                    Cookies.set('permissions', JSON.stringify(response.data.permissions));

                    console.log('账号信息', response.data)

                    Message.success('登录成功');
                    this.$router.push('/home');
                } else {
                    Message.error('登录失败，请检查用户名和密码');
                }
            } catch (error) {
                Message.error('登录失败，请稍后再试');
            }
        },
    }
};
</script>

<style scoped lang="scss">
.login {
    background-image: url('@/assets/bg4-2.png'), url('@/assets/111.png');
    background-size: cover;
    height: 100vh;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding-right: 10vh;
}

.login-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.login-header {
    background-image: url('@/assets/title.png');
    font-size: 46px;
    font-weight: bold;
    margin-bottom: 50px;
    letter-spacing: 5px;
    width: 650px;
    height: 50px;
    background-size: cover;

}

.login-container {
    border: 30px solid transparent;
    border-image: url('@/assets/border.png') 30 round;
    width: 650px;
    height: 500px;
    display: flex;
    box-sizing: border-box;
    padding: 40px;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    filter: brightness(1.95);

    span {
        width: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 45px;
        color: white;
        font-size: 25px;
        font-weight: bold;

        .login-input {
            margin-left: 20px;
            padding: 10px;
            background: url('@/assets/input.png') no-repeat center center;
            background-size: cover;
            border: none;
            border-radius: 5px;
            height: 40px;
            width: 90%;
            font-size: 24px;
            font-weight: bold;

            background-color: transparent;
            outline: none;
            letter-spacing: 2px;
            // filter: brightness(1.5);


        }
    }

}




input[type=text],
[type=password] {
    color: #858080;
}



.login-button {
    padding: 10px;
    border: none;
    color: #fff;
    border-radius: 5px;
    font-weight: bold;
    cursor: pointer;
    width: 150px;
    height: 60px;
    font-size: 22px;
    text-align: center;
    letter-spacing: 2px;
    background: url('@/assets/input.png') no-repeat center center;



}

.login-button:hover {
    transition: all linear 0.2s;
    filter: brightness(1.45);
}
</style>
